package com.lost.octopus.es.processor;

import com.google.common.collect.Lists;

import com.sunsharing.share.boot.framework.annotation.EnableFrameWork;
import com.lost.octopus.es.processor.config.es.alias.SimpleAliasElasticsearchRepository;
import com.lost.octopus.es.processor.util.ApplicationUtil;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.util.Map;

@SpringBootApplication
@EnableFrameWork
@EnableElasticsearchRepositories(
    // 自定义仓储,实现部分别名所需操作
    repositoryBaseClass = SimpleAliasElasticsearchRepository.class,
    elasticsearchTemplateRef = "aliasElasticsearchRestTemplate"
)
@MapperScan(basePackages = "com.lost.octopus.es.processor.dao.mapper.mybatis")
public class EsProcessorApplication implements ApplicationContextAware {

    public static void main(String[] args) {
        SpringApplication.run(EsProcessorApplication.class, args);
    }

    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationUtil.setApplicationContext(applicationContext);
    }

    public static <T> T getBean(Class<T> clazz) {
        Map<String, T> beans = EsProcessorApplication.applicationContext.getBeansOfType(clazz);
        if (beans.size() <= 0) {
            return null;
        }
        return Lists.newArrayList(beans.values()).get(0);
    }
}
